<template>
  <div>
    <!-- 表格组件 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="create_date" label="日期" width="120">
        <template slot-scope="scope">
          {{ moment(scope.row.create_date).format("YYYY-MM-DD") }}
        </template>
      </el-table-column>
      <el-table-column prop="user.nickname" label="姓名" width="160">
      </el-table-column>
      <el-table-column prop="create_date" label="封面" width="160">
        <template slot-scope="scope">
          <img
            v-if="scope.row.cover[0]"
            :src="scope.row.cover[0].url"
            class="cover"
          />
          <img v-else src="../assets/hyjpg.jpg" alt="" class="cover" />
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini">编辑</el-button>
          <el-button size="mini" type="danger"
            >{{ scope.row.id }}删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[2, 5, 8, 10]"
      :page-size="pageSize"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
// 导入时间转换的工具库
import moment from "moment";
export default {
  data() {
    return {
      moment,
      tableData: [],
      // 分页
      pageIndex: 1,
      pageSize: 5,
      total: 0,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    //封装请求文章的函数
    getList() {
      this.$axios({
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        console.log(res.data);
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      // 每当分页组件切换页面的时候, 就会触发
      // 本来分页组件啥都不会做, 只会把切换的页码给哦们传进来
      // 我们就需要手动加载响应页面的数据即可
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.cover {
  width: 50px;
  height: 40px;
  object-fit: cover;
  border: 1px solid #ccc;
  border-radius: 5px;
}
</style>
